/**
 * 
 */
package com.wubo.sec.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;


/**
 * @Author 吴博
 * 创建时间 Jul 12, 2010 1:54:56 PM
 * @Version 1.0.01
 */
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent>{
	
	protected Log log = LogFactory.getLog(AuthenticationSuccessListener.class);

	public void onApplicationEvent(AuthenticationSuccessEvent event) {
		if(event instanceof AuthenticationSuccessEvent){
			log.debug("Catch a AuthenticationSuccessEvent");
			Object obj = event.getSource();
			if(obj instanceof Authentication){
				Authentication auth = (Authentication) obj;
				log.debug("Authentication Details:"+auth.getDetails());
				
				onUserLoginSuccess((UserData) auth.getPrincipal(), HttpServletRequestHolder.getIP());
			}
		}
	}
	
	protected void onUserLoginSuccess(UserData user, String ip){
		java.util.Date lastLoginTime = user.getLastLoginTime();
		
		UserLoginProcessor loginProcessor = UserLoginPageHelper.getLoginProcessor();
		if( loginProcessor.supports(user.getClass()) ){
			loginProcessor.onUserLoginSuccess(user, ip);
		}
		
		user.setLastLoginTime(lastLoginTime);
	}

}
